"""
Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
"""

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    # @param {TreeNode} root
    # @return {integer}
    def minDepth(self, root):
        if not root:
            return 0

        l = self.minDepth(root.left)
        r = self.minDepth(root.right)
        
        if root.left and root.right:
            return min(l, r)+1
        if root.left:
            return l+1
        if root.right:
            return r+1

        return 1

